package com.wonderGirls.swGirls2;

import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;

import android.app.Activity;
import android.app.WallpaperManager;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.os.Bundle;
import android.util.FloatMath;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;
import android.widget.Toast;

import com.mobclick.android.MobclickAgent;
import com.wonderGirls.swGirls2.util.ResourceManager;

public class GirlGalleryActivity extends Activity implements OnTouchListener {
	ImageView imageView;
	TextView txtPage;
	ImageButton buttonPrevious;
	ImageButton buttonNext;

	int girlIndex;
	int imgCount;
	int currentIndex = 0;
	String dirName;

	// These matrices will be used to move and zoom image
	private static final String TAG = "Touch";
	Matrix matrix = new Matrix();
	Matrix savedMatrix = new Matrix();
	PointF start = new PointF();
	PointF mid = new PointF();
	float oldDist;
	float oldScale;
	DecimalFormat format;

	// We can be in one of these 3 states
	static final int NONE = 0;
	static final int DRAG = 1;
	static final int ZOOM = 2;
	int mode = NONE;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.girlgallery);
		imageView = (ImageView) findViewById(R.id.imageViewGirl);
		girlIndex = this.getIntent().getIntExtra("girlIndex", 0);
		imgCount = ResourceManager.indexToCountMap.get(girlIndex);
		txtPage = (TextView) this.findViewById(R.id.txtPage);
		format = new DecimalFormat("00");
		dirName = ResourceManager.indexToResDirMap.get(girlIndex);
		setImageView();

		buttonPrevious = (ImageButton) findViewById(R.id.buttonPrevious);
		buttonNext = (ImageButton) findViewById(R.id.buttonNext);

		buttonNext.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				currentIndex++;
				currentIndex = currentIndex % imgCount;
				setImageView();
			}
		});

		buttonPrevious.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				currentIndex--;
				if (currentIndex < 0) {
					currentIndex = imgCount - 1;
				}
				setImageView();
			}
		});

		imageView.setOnTouchListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuItem item = menu.add("设为壁纸");
		item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			@Override
			public boolean onMenuItemClick(MenuItem item) {
				setWallpaper();
				return true;
			}
		});
		return true;
	}

	/**
	 * 设置壁纸
	 */
	protected void setWallpaper() {
		WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
		String fileName = dirName + File.separator
				+ format.format((currentIndex + 1)) + ".jpg";
		try {
			wallpaperManager.setBitmap(ResourceManager
					.getBitmapFromAsset(fileName));
			Toast.makeText(this, "设置成功", Toast.LENGTH_SHORT).show();
		} catch (IOException e) {
			Toast.makeText(this, "设置失败", Toast.LENGTH_SHORT).show();
			e.printStackTrace();
		}

	}

	private void setImageView() {
		String fileName = dirName + File.separator
				+ format.format((currentIndex + 1)) + ".jpg";
		try {
			imageView.setImageBitmap(ResourceManager
					.getBitmapFromAsset(fileName));
		} catch (IOException e) {
			e.printStackTrace();
		}

		txtPage.setText((1 + currentIndex) + " of " + imgCount);
		imageView.setScaleType(ScaleType.FIT_CENTER);
		oldScale = 1f;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			this.finish();
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (event.getAction() & MotionEvent.ACTION_MASK) {
		case MotionEvent.ACTION_DOWN:
			savedMatrix.set(matrix);
			start.set(event.getX(), event.getY());
			Log.d(TAG, "mode=DRAG");
			mode = DRAG;
			break;

		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_POINTER_UP:
			mode = NONE;
			Log.d(TAG, "mode=NONE");
			break;
		case MotionEvent.ACTION_POINTER_DOWN:
			oldDist = spacing(event);
			Log.d(TAG, "oldDist=" + oldDist);
			if (oldDist > 10f) {
				savedMatrix.set(matrix);
				midPoint(mid, event);
				mode = ZOOM;
				Log.d(TAG, "mode=ZOOM");
			}
			break;
		case MotionEvent.ACTION_MOVE:
			if (mode == DRAG) {
				matrix.set(savedMatrix);
				matrix.postTranslate(event.getX() - start.x, event.getY()
						- start.y);
			} else if (mode == ZOOM) {
				float newDist = spacing(event);
				Log.d(TAG, "newDist=" + newDist + "   oldDist=" + oldDist
						+ "  scale=" + newDist / oldDist);
				if (newDist > 10f) {
					imageView.setScaleType(ScaleType.MATRIX);
					matrix.set(savedMatrix);
					float scale = newDist / oldDist;
					matrix.postScale(scale, scale, mid.x, mid.y);
				}
			}
			break;
		}

		// Perform the transformation
		imageView.setImageMatrix(matrix);

		return true;
	}

	private float spacing(MotionEvent event) {
		float x = event.getX(0) - event.getX(1);
		float y = event.getY(0) - event.getY(1);
		return FloatMath.sqrt(x * x + y * y);
	}

	private void midPoint(PointF point, MotionEvent event) {
		float x = event.getX(0) + event.getX(1);
		float y = event.getY(0) + event.getY(1);
		point.set(x / 2, y / 2);
	}

	// // ��ť����Ŵ���
	// private void enlarge() {
	// try {
	// int bmpWidth = bmp.getWidth();
	// int bmpHeight = bmp.getHeight();
	//
	// scaleWidth = (float) (scaleWidth * ZOOM_IN_SCALE);
	// scaleHeight = (float) (scaleHeight * ZOOM_IN_SCALE);
	//
	// Matrix matrix = new Matrix();
	// matrix.postScale(scaleWidth, scaleHeight);
	// zoomedBMP = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight,
	// matrix, true);
	// imageView.setImageBitmap(zoomedBMP);
	// } catch (Exception e) {
	// // can't zoom because of memory issue, just ignore, no big deal
	// }
	//
	// }

	// // ��ť�����С����
	// private void small() {
	//
	// int bmpWidth = bmp.getWidth();
	// int bmpHeight = bmp.getHeight();
	//
	// scaleWidth = (float) (scaleWidth * ZOOM_OUT_SCALE);
	// scaleHeight = (float) (scaleHeight * ZOOM_OUT_SCALE);
	//
	// Matrix matrix = new Matrix();
	// matrix.postScale(scaleWidth, scaleHeight);
	// zoomedBMP = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix,
	// true);
	// imageView.setImageBitmap(zoomedBMP);
	// }
}
